package com.xms.utils;


import cn.hutool.core.date.DateUtil;
import com.xms.core.enums.DateInterval;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * DateTimeUtil
 * </p>
 *
 * @author migo
 * @since 2021/9/3
 */
public class DateTimeUtil {//extends DateUtils
    public static final String DATEFORMATE_YMD = "yyyyMMdd";
    private static String[] parsePatterns = {
            "yyyy-MM-dd",
            "yyyy-MM-dd HH:mm:ss",
            "yyyy-MM-dd HH:mm",
            "yyyy-MM",
            "yyyy/MM/dd",
            "yyyy/MM/dd HH:mm:ss",
            "yyyy/MM/dd HH:mm",
            "yyyy/MM",
            "yyyy.MM.dd",
            "yyyy.MM.dd HH:mm:ss",
            "yyyy.MM.dd HH:mm",
            "yyyy.MM"};

//    public static Date parse(String s) {
//        if (s == null || s.toString().isEmpty()) {
//            return null;
//        }
////        try {
////            return parseDate(s, parsePatterns);
////        } catch (ParseException e) {
////            return null;
////        }
//        return null;
//    }

    /**
     * @param s                  指定日期字符串
     * @param parsePatternsIndex 指定日期格式。从 0 开始
     * @return
     */
    public static Date parse(String s, int parsePatternsIndex) {
        if (s == null || s.toString().isEmpty()) {
            return null;
        }
        try {
            return new SimpleDateFormat(parsePatterns[parsePatternsIndex]).parse(s);
        } catch (Exception e) {
            return null;
        }
    }

    public static long dateDiff(int interval, Date date1, Date date2) // DateInterval interval
    {
//        TimeSpan ts = date2 - date1;
        switch (interval) {
            case DateInterval.Year:
                return DateUtil.year(date2) - DateUtil.year(date1);
            case DateInterval.Month:
//                return (date2.Month - date1.Month) + (12 * (date2.Year - date1.Year));
                return (DateUtil.month(date2) - DateUtil.month(date1)) + (12 * (DateUtil.year(date2) - DateUtil.year(date1)));
            case DateInterval.Weekday:
//                return Fix(ts.TotalDays) / 7;
                return DateUtil.betweenDay(date2, date1, false) / 7;

            case DateInterval.Day:
//                return Fix(ts.TotalDays);
                return DateUtil.betweenDay(date2, date1, false);

            case DateInterval.Hour:
//                return Fix(ts.TotalHours);
                return date2.getTime() / (1000 * 60 * 60) - date1.getTime() / (1000 * 60 * 60); // 毫秒数/1000/60/60

            case DateInterval.Minute:
//                return Fix(ts.TotalMinutes);
                return DateUtil.betweenMs(date2, date1);

            default:
//                return Fix(ts.TotalSeconds);
                return date2.getTime() / 1000 - date1.getTime() / 1000; // 毫秒数/1000
        }
    }

    private static long Fix(double Number) {
        if (Number >= 0) {
            return (long) Math.floor(Number);
        }
        return (long) Math.ceil(Number);
    }
}
